//
// Created by steven on 2017/2/27.
// Copyright (c) 2017 steven. All rights reserved.
//

#import "GMProfileHeaderView.h"

#define AVATAR_WIDTH 80
@implementation GMProfileHeaderView

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupMainView];
    }

    return self;
}

- (void)setupMainView {
    self.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.avatarImageView];
    [self addSubview:self.nameLabel];
    [self addSubview:self.scoreImageView];
    [self addSubview:self.scoreLabel];
    [self addSubview:self.guardImageView];
    [self addSubview:self.guardLabel];
    [self addSubview:self.subUsersImageView];
    [self addSubview:self.subUsersLabel];

    // avatar image view
    [self.avatarImageView autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:40];
    [self.avatarImageView autoAlignAxisToSuperviewAxis:ALAxisVertical];
    self.avatarImageView.layer.cornerRadius = AVATAR_WIDTH/2;
    [self.avatarImageView autoSetDimensionsToSize:CGSizeMake(AVATAR_WIDTH, AVATAR_WIDTH)];
    // add action
    [self.avatarImageView setUserInteractionEnabled:YES];
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(avatarViewTapped)];
    [singleTap setNumberOfTapsRequired:1];
    [self.avatarImageView addGestureRecognizer:singleTap];

    [self.nameLabel autoAlignAxisToSuperviewAxis:ALAxisVertical];
    [self.nameLabel autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.avatarImageView withOffset:8];
    
    // guard label
    [self.guardLabel autoAlignAxisToSuperviewAxis:ALAxisVertical];
    [self.guardLabel autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:16];
    
    // score label
    [self.scoreLabel autoAlignAxis:ALAxisHorizontal toSameAxisOfView:self.guardLabel];
    [self.scoreLabel autoPinEdgeToSuperviewEdge:ALEdgeLeading withInset:40];
    
    // subuser label
    [self.subUsersLabel autoAlignAxis:ALAxisHorizontal toSameAxisOfView:self.guardLabel];
    [self.subUsersLabel autoPinEdgeToSuperviewEdge:ALEdgeTrailing withInset:40];
    
    // guard icon
    [self.guardImageView autoAlignAxisToSuperviewAxis:ALAxisVertical];
    [self.guardImageView autoPinEdge:ALEdgeBottom toEdge:ALEdgeTop ofView:self.guardLabel withOffset:-4];
    
    // score icon
    [self.scoreImageView autoAlignAxis:ALAxisVertical toSameAxisOfView:self.scoreLabel];
    [self.scoreImageView autoPinEdge:ALEdgeBottom toEdge:ALEdgeTop ofView:self.scoreLabel withOffset:-4];
    
    // subuser icon
    [self.subUsersImageView autoAlignAxis:ALAxisVertical toSameAxisOfView:self.subUsersLabel];
    [self.subUsersImageView autoPinEdge:ALEdgeBottom toEdge:ALEdgeTop ofView:self.subUsersLabel withOffset:-4];
}

- (void)avatarViewTapped {
    if ([self.delegate respondsToSelector:@selector(avatarImageViewDidTapped)]) {
        [self.delegate avatarImageViewDidTapped];
    }
}

- (UIImageView *)avatarImageView {
    if (!_avatarImageView) {
        _avatarImageView = [[UIImageView alloc] init];
    }
    return _avatarImageView;
}

#pragma mark - getter for name label
- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.font = [UIFont systemFontOfSize:14];
    }
    return _nameLabel;
}

#pragma mark - getter for score image view
- (UIImageView *)scoreImageView {
    if (!_scoreImageView) {
        _scoreImageView = [[UIImageView alloc] init];
        _scoreImageView.image = [UIImage imageNamed:@"setting-score-icon"];
    }
    
    return _scoreImageView;
}

#pragma mark - getter for score label
- (UILabel *)scoreLabel {
    if (!_scoreLabel) {
        _scoreLabel = [[UILabel alloc] init];
        _scoreLabel.textColor = HINT_COLOR;
        _scoreLabel.font = [UIFont systemFontOfSize:10];
    }
    
    return _scoreLabel;
}

#pragma mark - getter for guard iamge view
- (UIImageView *)guardImageView {
    if (!_guardImageView) {
        _guardImageView = [[UIImageView alloc] init];
        _guardImageView.image = [UIImage imageNamed:@"setting-guard-icon"];
    }
    
    return _guardImageView;
}

#pragma mark - getter for guard label
- (UILabel *)guardLabel {
    if (!_guardLabel) {
        _guardLabel = [[UILabel alloc] init];
        _guardLabel.textColor = HINT_COLOR;
        _guardLabel.font = [UIFont systemFontOfSize:10];
    }
    
    return _guardLabel;
}

#pragma mark - getter for sub users image view
- (UIImageView *)subUsersImageView {
    if (!_subUsersImageView) {
        _subUsersImageView = [[UIImageView alloc] init];
        _subUsersImageView.image = [UIImage imageNamed:@"setting-subuser-icon"];
    }
    
    return _subUsersImageView;
}

#pragma mark - getter for sub users label
- (UILabel *)subUsersLabel {
    if (!_subUsersLabel) {
        _subUsersLabel = [[UILabel alloc] init];
        _subUsersLabel.textColor = HINT_COLOR;
        _subUsersLabel.font = [UIFont systemFontOfSize:10];
    }
    
    return _subUsersLabel;
}

@end
